package com.jamal2362.apktool.service;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.util.BitSet;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.Pair;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.files.ErrorTree;
import com.jamal2362.apktool.util.IconFactory;
import com.jamal2362.apktool.util.IconTypes;
import com.jamal2362.apktool.util.Settings;

public class FileItem extends ErrorTree implements ANTLRErrorListener
{
	private final File file;
	private Context context;

	public FileItem(String file)
	{
		this(new File(file));
	}

	public FileItem(File file)
	{
		super("", "");
		this.file = file;
	}

	@Override
	public String getMessage()
	{
		return file.getName();
	}

	@Override
	public String getDetails()
	{
		return file.getAbsolutePath();
	}

	@Override
	public void setIcon(ImageView icon)
	{
		if (this.context == null)
			this.context = icon.getContext();
		IconFactory icf = new IconFactory(this.context);
		icf.round(Settings.roundIcons);
		icon.setImageBitmap(icf.makeIcon(IconTypes.colorText, R.drawable.ic_text));
	}

	public boolean isError(int index)
	{
		synchronized (childs)
		{
			try
			{
				for (ErrorTree e : childs)
				{
					child c = (FileItem.child) e;
					if (index >= c.start && index <= c.stop)
						return true;
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return false;
	}

	public File getFile()
	{
		return file;
	}

	@Override
	public void click(View view)
	{
		editor.open(file);
	}

	@Override
	public void addChild(ErrorTree child)
	{
		if (!(child instanceof FileItem.child))
		{
			throw new RuntimeException("FileItem only can add FileItem.child!");
		}
		super.addChild(child);
	}

	@Override
	public void reset()
	{
		super.reset();
	}

	@Override
	public void syntaxError(Recognizer<?, ?> recognizer,
							final Object offendingSymbol,
							int line,
							int charPositionInLine,
							final String msg,
							RecognitionException e)
	{
		context = getContext();
		String ln = null;
		if (context == null)
			ln = "line";
		else
			ln = context.getString(R.string.linel);
		addChild(new child(msg, ln + " " + line, offendingSymbol));
	}

	@Override
	public void reportAttemptingFullContext(Parser recognizer,
											DFA dfa,
											int startIndex,
											int stopIndex,
											BitSet conflictingAlts,
											ATNConfigSet configs)
	{
		// TODO: Implement this method
	}

	@Override
	public void reportContextSensitivity(Parser recognizer,
										 DFA dfa,
										 int startIndex,
										 int stopIndex,
										 int prediction,
										 ATNConfigSet configs)
	{
		// TODO: Implement this method
	}

	@Override
	public void reportAmbiguity(Parser recognizer,
								DFA dfa,
								int startIndex,
								int stopIndex,
								boolean exact,
								BitSet ambigAlts,
								ATNConfigSet configs)
	{
		// TODO: Implement this method
	}

	private class child extends ErrorTree
	{
		final int start, stop;
		child(CharSequence msg, CharSequence details, final Object symbol)
		{
			super(msg, details);
			if (symbol instanceof Token)
			{
				Token tk = (Token) symbol;
				start = tk.getStartIndex();
				stop = tk.getStopIndex();
			}
			else if (symbol instanceof Pair)
			{
				Pair<Integer,Integer> ctx = (Pair<Integer,Integer>) symbol;
				start = ctx.a.intValue();
				stop = ctx.b.intValue();
			}
			else if (symbol instanceof ParserRuleContext)
			{
				ParserRuleContext ctx = (ParserRuleContext) symbol;
				start = ctx.start.getStartIndex();
				stop = ctx.stop.getStopIndex();
			}
			else
			{
				start = 0;
				stop = 0;
			}
		}

		@Override
		public void setIcon(ImageView icon)
		{
			IconFactory icf = new IconFactory(icon.getContext());
			icon.setImageBitmap(icf.makeIcon(Color.TRANSPARENT, R.drawable.ic_error, 48, 32, 0));
		}

		@Override
		public void click(View view)
		{
			editor.open(file, start, stop);
		}
	}
}
